/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doc2db.interfaces.action;

import doc2db.Keey;
import doc2db.UserProp;
import doc2db.fileWorker.FileBeginer;
import doc2db.fileWorker.FileWorker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JOptionPane;

/**
 *
 * @author yuriy
 */
public class ChangeUserPropAction implements ActionListener{
    int what ;

    /**
     *
     * @param w 1 = user, 2 = pswd
     */
    public ChangeUserPropAction(int w){
        what = w;
    }

    public void actionPerformed(ActionEvent e) {
        String s = "";
        if(what == 1){
            String str = JOptionPane.showInputDialog(null, "Введите пароль",
                    "подтверждение", JOptionPane.WARNING_MESSAGE);
            if(str.equals(UserProp.pswd)){
                s = UserProp.user;
                UserProp.user = "" + JOptionPane.showInputDialog(null, "Введите новое имя",
                    "подтверждение", JOptionPane.DEFAULT_OPTION);
            }else{
                JOptionPane.showMessageDialog(null, "Неверный пароль", "ошибка",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }

            if( FileWorker.isInit(
                    FileBeginer.resurse_users_usersFolder+Keey.sp+FileBeginer.useListName,
                    UserProp.user)){
                JOptionPane.showMessageDialog(null, "Такой пользователь уже существует"
                    , "error! ",JOptionPane.ERROR_MESSAGE);
                return;
            }

            File file = new File(FileBeginer.resurse_users_usersFolder
                + Keey.sp + s);
            boolean success = file.renameTo(new File(
                    new File(FileBeginer.resurse_users_usersFolder).getAbsolutePath(),
                    UserProp.user));
            if(!success){
                UserProp.user = "" + s;
                JOptionPane.showMessageDialog(null, "Неудалось сменить пользователя",
                        "ошибка ", JOptionPane.ERROR_MESSAGE);
                return;
            }
            String [][] arr = FileWorker.getProprtyList(FileBeginer.resurse_users_usersFolder
                    + Keey.sp + FileBeginer.useListName);
            for (int i = 0; i < arr.length; i++) {
                if(arr[i][0].equals(s)){
                    arr[i][0] = "" + UserProp.user;
                    break;
                }
            }
            FileWorker.reWriteAllPropertyFile(FileBeginer.resurse_users_usersFolder
                    + Keey.sp + FileBeginer.useListName, arr);
        }else if(what == 2){
            String str = JOptionPane.showInputDialog(null, "Введите пароль",
                    "подтверждение", JOptionPane.WARNING_MESSAGE);
            if(str.equals(UserProp.pswd)){
                s = UserProp.pswd ;
                UserProp.pswd = JOptionPane.showInputDialog(null, "Введите новый пароль",
                    "подтверждение", JOptionPane.DEFAULT_OPTION);
                
                String [][] arr = FileWorker.getProprtyList(FileBeginer.resurse_users_usersFolder
                    + Keey.sp + FileBeginer.useListName);
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i][1].equals(Keey.decodStr(s))) {
                        arr[i][1] = "" + Keey.decodStr(UserProp.pswd);
                        break;
                    }
                }
                FileWorker.reWriteAllPropertyFile(FileBeginer.resurse_users_usersFolder
                        + Keey.sp + FileBeginer.useListName, arr);
            }else{
                JOptionPane.showMessageDialog(null, "Неверный пароль", "ошибка",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }

}
